<?php
require("init.php");
if (!isset($_SESSION['userid']))
{
    $template->assign("loginerror", 0);
    $template->display("login.tpl");
    die();
}

if ('-dicomproyecto' != $_REQUEST['key']) {
	session_destroy();
	header("Location: index.php");
}

require("config/standard/config.php");

mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);

mysql_query("set names 'utf8'");

if (isset($_REQUEST['enviar'])) {

	if (empty($_REQUEST['curso'])) {
		?>
		debe ingresar el curso <a href="<?php echo $_SERVER['PHP_SELF']; ?>?key=-dicomproyecto">volver</a>
		<?php
	    exit();
	}
	
	if (0 == count($_REQUEST['curso'])) {
	   
		?>
		debe ingresar el curso <a href="<?php echo $_SERVER['PHP_SELF']; ?>?key=-dicomproyecto">volver</a>
		<?php
	    exit();
	}
	
	$start = time();
	
	$end = $start + 30 * 24 * 60 * 60;
	$sql = "INSERT INTO projekte (name, start, end, status, budget, category) VALUES ('{$_REQUEST['curso']}', '{$start}', '{$end}', 1, 0, '{$_REQUEST['categoria']}')";
	echo $sql;
	mysql_query($sql);
	$idcurso = mysql_insert_id();
	
	foreach ($_REQUEST['usuario'] as $value){
		$sql = "INSERT INTO projekte_assigned (user, projekt) values ('{$value}', '{$idcurso}')";
		mysql_query($sql);
	}
	
	$sql = "SELECT * FROM tasklist WHERE project = '5'";
	$query = mysql_query($sql);
	
	while ($data = mysql_fetch_array($query)) {
		$datestart = time();
		$texto = htmlentities($data['name'], ENT_NOQUOTES, 'UTF-8');
        $texto = htmlspecialchars_decode($texto, ENT_NOQUOTES);
		$sql = "INSERT INTO tasklist VALUES ('', '{$idcurso}', '{$texto}', '{$data['desc']}', '{$datestart}', 1, 0, 0)";
		
		mysql_query($sql) or die(mysql_error());
		
		$idtask = mysql_insert_id();
		
		$sql = "SELECT * FROM tasks WHERE liste = '{$data['ID']}'";
		$querytask = mysql_query($sql) or die(mysql_error());

		
		while ($datatask = mysql_fetch_array($querytask)) {
			$texto = htmlentities($datatask['title'], ENT_NOQUOTES, 'UTF-8');
        	$texto = htmlspecialchars_decode($texto, ENT_NOQUOTES);
        	
        	$texto2 = htmlentities($datatask['text'], ENT_NOQUOTES, 'UTF-8');
        	$texto2 = htmlspecialchars_decode($texto, ENT_NOQUOTES);
			$sql = "INSERT INTO tasks (start, end, title, text, liste, status, project) VALUES ('{$start}', '{$end}', '{$texto}', '{$texto2}', '{$idtask}', '1', '{$idcurso}')";
			mysql_query($sql) or die(mysql_error());
			$idtasklist = mysql_insert_id();
			
			foreach ($_REQUEST['usuario'] as $value){
				$sql = "INSERT INTO tasks_assigned (user, task) values ('{$value}', '{$idtasklist}')";
				mysql_query($sql) or die(mysql_error());
			}
		}
	}
	
	
	/**
	 * Clonacion de carpetas
	 */
	
	$sql = "SELECT * FROM projectfolders WHERE project = '5'";
	$queryfolder = mysql_query($sql);
	
	while ($datafolder = mysql_fetch_array($queryfolder)) {
		$sql = "INSERT INTO projectfolders (parent, project, name) VALUES (0, '{$idcurso}', '{$datafolder['name']}')";
		mysql_query($sql) or die(mysql_error());
		
		 // create the folder
            $makefolder = CL_ROOT . "/files/" . CL_CONFIG . "/{$idcurso}/{$datafolder['name']}/";
            if (!file_exists($makefolder))
            {
                if (mkdir($makefolder, 0777, true))
                {
                   echo "carpeta {$datafolder['name']} creada correctamente <br />" ;
                }
            }
           
	}
	
	echo "<h2>CURSO CLONADO</h2>";
	
}else {

?>
<form name="cloneform" id="cloneform" method="POST">
<table>
    <caption><h2><font color="Red">Por favor Verifique que el curso existe antes de clonarlo</font></h2></caption>
    <tr>
        <td>Nombre Curso</td>
        <td><input type="text" size="40" name="curso" id="curso" ></td>
    </tr>
     <tr>
        <td>Categoria</td>
        <td><select name="categoria" id="categoria">
                <?php
                $sql = "SELECT id, name FROM category ORDER BY name";
                $query = mysql_query($sql);
                
                while ($data = mysql_fetch_array($query)) {
                	?>
                	<option value="<?php echo $data['id'] ?>"><?php echo $data['name'] ?></option>
                	<?php
                }
                ?>
            </select></td>
    </tr>
    <tr>
        <td>Usuarios</td>
        <td>
            <select name="usuario[]" id="usuario[]" size="50" multiple>
                <?php
                $sql = "SELECT ID, name FROM user ORDER BY name";
                $query = mysql_query($sql);
                
                while ($data = mysql_fetch_array($query)) {
                	?>
                	<option value="<?php echo $data['ID'] ?>"><?php echo $data['name'] ?></option>
                	<?php
                }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" name="enviar" id="enviar" value="Clonar">
            <input type="hidden" name="key" id="key" value="-dicomproyecto" >
        </td>
    </tr>
</table>
</form>
<?php
}
?>